package com.controller;

import com.po.Activity;
import com.po.Type;
import com.service.ActivityService;
import com.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TypeController {
    @Autowired
    private TypeService typeService;

    @Autowired
    private ActivityService activityService;
    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 8, sort = {"id"}, direction = Sort.Direction.DESC) @PathVariable Long id, Model model) {
        List<Type> types = typeService.findAll();
        if (id == -1) {
            id = types.get(0).getId();
        }

        model.addAttribute("types", types);
        List<Activity> activities=activityService.findAllByTypeId(id);
        model.addAttribute("page", activities);
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
